package yxy.game.pm2.active.handle;

import yxy.cherry.battle.module.Position;
import yxy.cherry.battle.module.record.FighterSide;
import yxy.cherry.battle.module.record.result.StatisticFighter;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.atcholidayBOSS;
import yxy.cherry.data.bean.atcholidaypreview;
import yxy.cherry.data.bean.herodata;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Format.FormatType;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.BattleMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.battle.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

/**
 * Author HYB
 * 节日挑战BOSS
 */
public class Act_HolidayBossChallengeHandle extends BattleModuleHandle {

    @Override
    protected String code() {
        return BattleType.ACT_HOLIDAYCHALLENGE.name();
    }

    /**
     * 处理战斗
     */
    public Result battleResponseExecute(String playerCode, atcholidayBOSS boss, ActiveMessage.ActHolidayChallengeResponse.Builder response)
            throws Exception {

        BattleHandle battleHandle = new BattleHandle();
        BattleModule battleModule = BattleModule.create(playerCode);
        battleModule.setType(BattleType.ACT_HOLIDAYCHALLENGE);
        battleModule.setCode(code());
        // 玩家的team
        TeamModule teamA = battleHandle.teamPlayer(playerCode, FormatType.普通);
        battleModule.setTeamA(teamA);

        // 玩家的team
        TeamModule teamB = teamBoss(boss);
        battleModule.setTeamB(teamB);

        BattleMessage.BattleExecuteResponse res = new BattleHandle().battleResponseExecute(battleModule //
                , boss.getRound(), false, null);
        // 计算伤害
        long damage = 0;
        for (StatisticFighter fighter : battleModule.getStatistics()) {
            if (fighter.getPos().getSide().equals(FighterSide.RIGHT)) {
                damage += fighter.getHut();
            }
        }

        Collection<Currency> rewards = handleDamage(damage);

        // 封装消息
        response.setBattleResp(res);// 录像
        response.setBattleResult(battleModule.getBattleResult().getNumber());// 战果
        response.setDamage(damage);
        if (rewards != null) {
            response.addAllRewards(new CurrencyHandle().packItems(rewards));
            new CurrencyHandle().addCurrency(playerCode, CCReason.活动_节日_挑战BOSS, rewards);
        }

        return Result.Success;
    }

    /**
     * 处理伤害
     */
    private Collection<Currency> handleDamage(long damage) {
        Map<String, atcholidaypreview> allData = DataCenter.getAllData(atcholidaypreview.class);
        ArrayList<atcholidaypreview> atcholidaypreviews = new ArrayList<>(allData.values());
        atcholidaypreviews.sort((o1, o2) -> o2.getHurt() > o1.getHurt() ? 1 : -1);
        Collection<Currency> reward = null;
        for (atcholidaypreview atc : atcholidaypreviews) {
            if (damage > atc.getHurt()) {
                reward = Currency.create(atc.getRewardtype(), atc.getRewardid(), atc.getRewardnum());
                break;
            }
        }
        return reward;
    }

    public TeamModule teamBoss(atcholidayBOSS boss) {
        TeamModule team = new TeamModule();
        // 武将
        Position position = Position.FM;
        int heroid = boss.getFront();
        int level = boss.getLevel();
        int anger = 0;

        FighterModule fighterModule = FighterModule.create(heroid, level, 0, 0, anger, new FighterModule.InitHpResidual(boss.getLife(), boss.getLife()));
        fighterModule.getProperty().setLife(boss.getLife());
        fighterModule.getProperty().setLifeAdd(0);
        team.addfighter(position, fighterModule);

        // 信息
        herodata _herodata = DataCenter.getData(heroid, herodata.class);
        team.setName(_herodata.getName());
        team.setHead(boss.getHeadid());
        team.setPower(team.power());
        return team;
    }
}
